<template>
  <div class="app-container">
    <el-card style="border-radius: 10px">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="SN码" prop="snCode">
          <el-input
            v-model="queryParams.snCode"
            placeholder="请输入SN码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="产品物料编码" prop="itemCode">
          <el-input
            v-model="queryParams.itemCode"
            placeholder="请输入产品物料编码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="产品物料名称" prop="itemName">
          <el-input
            v-model="queryParams.itemName"
            placeholder="请输入产品物料名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="批次号" prop="batchCode">
          <el-input
            v-model="queryParams.batchCode"
            placeholder="请输入批次号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <div style="height: 15px"></div>
    <el-card style="border-radius: 10px">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['mes:wm:sn:add']"
          >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['mes:wm:sn:export']"
          >导出全部
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="snList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="产品编码" align="center" prop="itemCode"/>
        <el-table-column label="产品名称" align="center" prop="itemName">
          <template slot-scope="scope">
            <el-tag type="primary">{{ scope.row.itemName }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="规格型号" align="center" prop="specification"/>
        <el-table-column label="单位" align="center" prop="unitOfMeasure"/>
        <el-table-column label="批次号" align="center" prop="batchCode">
          <template slot-scope="scope">
            <el-tag type="success">{{ scope.row.batchCode }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="SN码数量" align="center" prop="snNum">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="handleView(scope.row)"
              v-hasPermi="['mes:wm:sn:query']"
            >{{ scope.row.snNum }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="生成时间" align="center" prop="genDate">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.genDate, '{y}-{m}-{d} {h}:{mi}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-download"
              @click="handleExport(scope.row)"
              v-hasPermi="['mes:wm:sn:export']"
            >导出
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['mes:wm:sn:remove']"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改SN码对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="物料编码" prop="itemCode">
              <el-input v-model="form.itemCode" readonly="readonly" placeholder="请选择物料编码">
                <el-button slot="append" @click="handleSelectProduct" icon="el-icon-search"></el-button>
              </el-input>
            </el-form-item>
            <ItemSelect ref="itemSelect" @onSelected="onItemSelected"></ItemSelect>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料名称" prop="itemName">
              <el-input v-model="form.itemName" readonly="readonly"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="规格型号" prop="specification">
              <el-input v-model="form.specification" type="textarea" readonly="readonly"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="入库批次号" prop="batchCode">
              <el-input v-model="form.batchCode" placeholder="请输入入库批次号"/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label-width="80">
              <el-switch v-model="autoGenFlag"
                         active-color="#13ce66"
                         active-text="自动生成"
                         @change="handleAutoGenChange(autoGenFlag)" v-if="optType != 'view'">
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="SN码数量" prop="snNum">
              <el-input-number :min="1" :max="1000" v-model="form.snNum" placeholder="请输入需要生成的SN码数量"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog title="SN码列表" :visible.sync="dialogVisible" width="960px" append-to-body>
      <el-table :data="snDetailsList" style="width: 100%">
        <el-table-column prop="itemCode" label="产品编码" align="center"></el-table-column>
        <el-table-column prop="itemName" label="产品名称" align="center">
          <template slot-scope="scope">
            <el-tag type="primary">{{ scope.row.itemName }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="specification" label="产品规格" align="center"></el-table-column>
        <el-table-column prop="batchCode" label="产品批次" align="center">
          <template slot-scope="scope">
            <el-tag type="success">{{ scope.row.batchCode }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="snCode" label="条形码" width="260" align="center">
          <template slot-scope="scope">
            <barcode :value="scope.row.snCode" height="30" width="1" font-size="16" margin="1px">
              Show this if the rendering fails
            </barcode>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="snDetailstotal>0"
        :total="snDetailstotal"
        :page.sync="snDetailsQueryParams.pageNum"
        :limit.sync="snDetailsQueryParams.pageSize"
        @pagination="getSnDetailsList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleClose">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addSn, delSn, getSn, list, listSn } from "@/api/mes/wm/sn";
import ItemSelect from "@/components/itemSelect/single.vue";
import { genCode } from "@/api/system/autocode/rule";
import VueBarcode from 'vue-barcode';

export default {
  name: "Sn",
  components: {ItemSelect, 'barcode': VueBarcode},
  data() {
    return {
      dialogVisible: false,
      autoGenFlag: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // SN码表格数据
      snList: [],
      snDetailsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        snCode: null, itemId: null, itemCode: null, itemName: null, specification: null, unitOfMeasure: null, batchCode: null,
      },
      snDetailsQueryParams: {
        pageNum: 1,
        pageSize: 10,
        genDate: null,
      },
      snDetailstotal: 0,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        snNum: [
          {required: true, message: "请输入需要生成的SN码数量", trigger: "blur"}
        ],
        itemCode: [
          {required: true, message: "产品物料不能为空", trigger: "blur"}
        ],
        batchCode: [
          {required: true, message: "批次码不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询SN码列表 */
    getList() {
      this.loading = true;
      listSn(this.queryParams).then(response => {
        debugger;
        this.snList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        snId: null,
        snCode: null,
        itemId: null,
        itemCode: null,
        itemName: null,
        specification: null,
        unitOfMeasure: null,
        batchCode: null,
        remark: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        genDate: null
      };
      this.resetForm("form");
      this.autoGenFlag = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.snId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    //点开查看界面
    handleView(row) {
      this.snDetailsList = [];
      this.dialogVisible = true;
      this.snDetailsQueryParams.genDate = row.genDate;
      this.getSnDetailsList();
    },
    getSnDetailsList() {
      list(this.snDetailsQueryParams).then(response => {
        this.snDetailsList = response.rows;
        this.snDetailstotal = response.total;
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "生成SN码";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const snId = row.snId || this.ids
      getSn(snId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改SN码";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addSn(this.form).then(response => {
            this.$modal.msgSuccess("生成成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const snIds = row.genDate;
      this.$modal.confirm('是否确认删除这' + row.snNum + '条SN码数据项？').then(function () {
        return delSn(snIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport(row) {
      this.queryParams.itemCode = row.itemCode;
      this.queryParams.gen_date = row.genDate;
      this.download('/mes/wm/sn/export', {
        ...this.queryParams
      }, `sn_${new Date().getTime()}.xlsx`)
    },
    handleSelectProduct() {
      this.$refs.itemSelect.showFlag = true;
    },
    //物料选择弹出框
    onItemSelected(obj) {
      debugger;
      if (obj != undefined && obj != null) {
        this.form.itemId = obj.itemId;
        this.form.itemCode = obj.itemCode;
        this.form.itemName = obj.itemName;
        this.form.specification = obj.specification;
        this.form.unitOfMeasure = obj.unitOfMeasure;
      }
    },
    handleAutoGenChange(autoGenFlag) {
      if (autoGenFlag) {
        genCode('BATCH_CODE_NEW').then(response => {
          this.form.batchCode = response;
        });
      } else {
        this.form.batchCode = null;
      }
    },
    handleClose() {
      this.dialogVisible = false;
    },
  }
};
</script>
